package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理设备报警数据
     *
     * @param handleAlertDataDto
     * @return
     */
    @Override
    public int handleAlertData(HandleAlertDataDto handleAlertDataDto) {
        AlertData alertData = BeanUtil.toBean(handleAlertDataDto, AlertData.class);
        alertData.setStatus(1);
        Long userId = SecurityUtils.getUserId();
        alertData.setProcessorId(userId);
        // 查询处理人名字
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        alertData.setProcessorName(sysUser.getNickName());
       return this.updateById(alertData) ? 1 : 0;
    }

    /**
     * 条件分页查询
     *
     * @param alertData
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Page queryAlertDataList(AlertData alertData, LocalDateTime startTime, LocalDateTime endTime
                                        ,Integer pageNum, Integer pageSize) {
        Page<AlertData> page = Page.of(pageNum, pageSize);
        page.addOrder(OrderItem.asc("create_time"));
        String deviceName = alertData.getDeviceName();
        Integer status = alertData.getStatus();

        page = lambdaQuery().like(ObjectUtil.isNotEmpty(deviceName), AlertData::getDeviceName, deviceName)
                .eq(ObjectUtil.isNotNull(status), AlertData::getStatus, status)
                .between(ObjectUtil.isNotEmpty(startTime) && ObjectUtil.isNotEmpty(endTime), AlertData::getCreateTime, startTime, endTime)
                .page(page);
        return page;
    }
}
